package com.xqq.weather;

import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.generators.Ed25519KeyPairGenerator;
import org.bouncycastle.crypto.params.Ed25519KeyGenerationParameters;
import org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters;
import org.bouncycastle.crypto.params.Ed25519PublicKeyParameters;
import java.security.SecureRandom;

public class Ed25519KeyGenerator {

    public static void main(String[] args) {
        // 生成密钥对
        AsymmetricCipherKeyPair keyPair = generateKeyPair();

        // 获取私钥和公钥
        Ed25519PrivateKeyParameters privateKey = (Ed25519PrivateKeyParameters) keyPair.getPrivate();
        Ed25519PublicKeyParameters publicKey = (Ed25519PublicKeyParameters) keyPair.getPublic();

        // 打印密钥（Base64编码）
        System.out.println("Private Key: " + java.util.Base64.getEncoder().encodeToString(privateKey.getEncoded()));
        System.out.println("Public Key: " + java.util.Base64.getEncoder().encodeToString(publicKey.getEncoded()));
    }

    public static AsymmetricCipherKeyPair generateKeyPair() {
        // 使用安全随机数生成器
        SecureRandom random = new SecureRandom();

        // 初始化密钥对生成器
        Ed25519KeyPairGenerator generator = new Ed25519KeyPairGenerator();
        generator.init(new Ed25519KeyGenerationParameters(random));

        // 生成密钥对
        return generator.generateKeyPair();
    }
}
